﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;

namespace MidSoft.ProjectInfo
{
    public partial class Modify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CKEditorControl1.config.toolbar = new object[]
			{
				new object[] { "Source" },
				new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord" },
				new object[] { "Undo", "Redo", "-", "Find", "Replace", "-", "SelectAll", "RemoveFormat" },
				new object[] { "Form", "Checkbox", "Radio", "TextField", "Textarea", "Select", "Button" },
                new object[] { "Bold", "Italic", "Underline", "Strike"},
				"/",
                new object[] { "Link", "Unlink", "Anchor"},
				new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote" },
				new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
				new object[] { "BidiLtr", "BidiRtl" ,"Maximize", "ShowBlocks"},
                "/",
				new object[] { "Table", "HorizontalRule", "SpecialChar", "PageBreak", "Iframe","Styles", "Format", "Font", "FontSize","TextColor", "BGColor" }
			};
            if (!Page.IsPostBack)
            {
                //设置部门
                BindDepartMentData();
                ddlDepartMent.DataBind();
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int ProjectInfoId = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(ProjectInfoId);
                }
            }
        }

        private void BindDepartMentData()
        {
            ChinaSoft.Model.Model_tb_MidSoft_DepartMent departments = new ChinaSoft.Model.Model_tb_MidSoft_DepartMent();
            ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent departmentsMgr = new ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent();
            ChinaSoft.Common.BindControl.BindDropDownList(ddlDepartMent, departmentsMgr.GetAllList(), "DepartMentName", "DepartMentNumber");
        }
        private void ShowInfo(int ProjectInfoId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bll = new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo model = bll.GetModel(ProjectInfoId);
            //MessageBox.Show(this,model.DepartMent.ToString());
            this.ddlDepartMent.SelectedIndex = this.ddlDepartMent.Items.IndexOf(ddlDepartMent.Items.FindByText(model.DepartMent));
            this.txtProjectNum.Text = model.ProjectNumber;
            this.txtProjectName.Text = model.ProjectName;
            this.ddlProjectSpecialty.SelectedIndex = this.ddlProjectSpecialty.Items.IndexOf(ddlProjectSpecialty.Items.FindByText(model.ProjectSpecialty));
            this.ddlMarker.SelectedIndex = this.ddlMarker.Items.IndexOf(ddlMarker.Items.FindByText(model.Marker));
            this.txtArea.Text = model.Area;
            this.txtCustomer.Text = model.Customer;
            this.ddlProperty.SelectedIndex = this.ddlProperty.Items.IndexOf(ddlProperty.Items.FindByText(model.Property));
            this.CKEditorControl1.Text = model.Description;
            this.ddlState.SelectedValue = model.ProjectState.ToString();
        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtProjectName.Text.Trim().Length == 0)
            {
                strErr += "项目名称不能为空！\\n";
            }
           
            if (this.txtArea.Text.Trim().Length == 0)
            {
                strErr += "区域不能为空！\\n";
            }
            if (this.txtCustomer.Text.Trim().Length == 0)
            {
                strErr += "业主单位不能为空！\\n";
            }
           
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int ProjectInfoId = ChinaSoft.Common.Input.SetInt(Request.Params["id"]);
            string ProjectName = this.txtProjectName.Text;
            string ProjectNumber = this.txtProjectNum.Text;
            string ProjectSpecialty = this.ddlProjectSpecialty.SelectedItem.Text;
            string DepartMent = this.ddlDepartMent.SelectedItem.Text;

            string Marker = this.ddlMarker.SelectedItem.Text;;
            string Area = this.txtArea.Text;
            string Customer = this.txtCustomer.Text;
            string Property = this.ddlProperty.SelectedItem.Text;
            string Description = this.CKEditorControl1.Text;
            string state = this.ddlState.SelectedValue;
            #region 修改
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bll = new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo model = bll.GetModel(ProjectInfoId);
            model.DepartMent=DepartMent;
            model.ProjectNumber = ProjectNumber;
            model.ProjectName = ProjectName;
            model.ProjectSpecialty = ProjectSpecialty;
            model.Marker = Marker;
            model.Area = Area;
            model.Customer = Customer;
            model.Property = Property;
            model.Description = Description;
            model.ProjectState = state;
            bll.Update(model);
            #endregion
            #region 修改操作日志
            ChinaSoft.Model.Model_tb_MidSoft_log modelLog = new ChinaSoft.Model.Model_tb_MidSoft_log();
            ChinaSoft.BLL.BLL_tb_MidSoft_log bllLog = new ChinaSoft.BLL.BLL_tb_MidSoft_log();
            modelLog.userAccount = "";
            if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                modelLog.userAccount = Session["UserAccount"].ToString();
            modelLog.userName = "";
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                modelLog.userName = Session["UserName"].ToString();
            modelLog.description = "项目信息修改成功！项目名称：“" + ProjectName + "”项目编号："+ProjectNumber;
            modelLog.LogType = 2;
            modelLog.CreateTime = DateTime.Now;
            bllLog.Add(modelLog);
            #endregion

            #region 修改系统消息
            ChinaSoft.Model.Model_tb_Midsoft_SysMessage modelSysMessage = new ChinaSoft.Model.Model_tb_Midsoft_SysMessage();
            ChinaSoft.BLL.BLL_tb_Midsoft_SysMessage bllSysMessage = new ChinaSoft.BLL.BLL_tb_Midsoft_SysMessage();
            modelSysMessage.CreateTime = DateTime.Now;
            modelSysMessage.CreateUserId = 0;
            modelSysMessage.CreateUserName = "";
            if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                modelSysMessage.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                modelSysMessage.CreateUserName = ChinaSoft.Common.Input.SetString(Session["UserName"]);
            modelSysMessage.Descript = "修改项目“"+ProjectName+"”成功!项目编号："+ProjectNumber;
            modelSysMessage.ProjectInfoId = ProjectInfoId;
            bllSysMessage.Add(modelSysMessage);
            #endregion

            #region 添加项目日志
            ChinaSoft.Model.Model_tb_Midsoft_ProjectLog modelProjectLog = new ChinaSoft.Model.Model_tb_Midsoft_ProjectLog();
            ChinaSoft.BLL.BLL_tb_Midsoft_ProjectLog bllProjectLog = new ChinaSoft.BLL.BLL_tb_Midsoft_ProjectLog();
            modelProjectLog.CreateTime = DateTime.Now;
            modelProjectLog.CreateUseId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            modelProjectLog.LogContent = "修改项目“"+ProjectName+"”成功！项目编号："+ProjectNumber;
            modelProjectLog.ProjectId = ProjectInfoId;
            bllProjectLog.Add(modelProjectLog);
            #endregion
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
        protected void ddlDepartMent_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetProjectNumber();
        }
        /// <summary>
        /// 设置项目编号
        /// </summary>
        private void SetProjectNumber()
        {
            string strZy = "";
            if (this.ddlProjectSpecialty.SelectedValue != "")
            {
                strZy = "0" + ddlProjectSpecialty.SelectedValue;
            }
            string strBh = System.DateTime.Now.Year + strZy + ddlDepartMent.SelectedValue;
            this.txtProjectNum.Text = strBh.ToString();
        }

        protected void ddlProjectSpecialty_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetProjectNumber();
        }
    }
}